<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\MktPost;

/**
 * MktPostSearch represents the model behind the search form of `common\models\MktPost`.
 */
class MktPostSearch extends MktPost
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'post_type', 'user_id', 'industry_id', 'area_id', 'status', 'is_anoymous', 'job_salary_min', 'job_salary_max', 'job_education', 'job_experience', 'job_age_min', 'job_age_max'], 'integer'],
            [['title', 'content', 'start_time', 'end_time', 'create_time', 'update_time', 'cooperation_reward'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $query = null)
    {
        if ( null === $query) {
            $query = MktPost::find()->orderBy(['id' => SORT_DESC]);
        }

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
        	    'pagesize' => 20,
            ],            
            
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'post_type' => $this->post_type,
            'user_id' => $this->user_id,
            'start_time' => $this->start_time,
            'end_time' => $this->end_time,
            'industry_id' => $this->industry_id,
            'area_id' => $this->area_id,
            'status' => $this->status,
            'is_anoymous' => $this->is_anoymous,
            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
            'job_salary_min' => $this->job_salary_min,
            'job_salary_max' => $this->job_salary_max,
            'job_education' => $this->job_education,
            'job_experience' => $this->job_experience,
            'job_age_min' => $this->job_age_min,
            'job_age_max' => $this->job_age_max,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'cooperation_reward', $this->cooperation_reward]);

        return $dataProvider;
    }

    public function searchWap($params, $query = null)
    {
        if ( null === $query) {
            $query = MktPost::find()
                ->where(['status' => MktPost::POST_STATUS_NORMAL])
                ->orderBy(['id' => SORT_DESC]);
        }

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
        	    'pagesize' => 7,
            ],            
            
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'post_type' => $this->post_type,
            'user_id' => $this->user_id,
            'start_time' => $this->start_time,
            'end_time' => $this->end_time,
            'industry_id' => $this->industry_id,
            'area_id' => $this->area_id,
            'status' => $this->status,
            'is_anoymous' => $this->is_anoymous,
            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
            'job_salary_min' => $this->job_salary_min,
            'job_salary_max' => $this->job_salary_max,
            'job_education' => $this->job_education,
            'job_experience' => $this->job_experience,
            'job_age_min' => $this->job_age_min,
            'job_age_max' => $this->job_age_max,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'cooperation_reward', $this->cooperation_reward]);

        return $dataProvider;
    }
    
}
